package com.cskaoyan.controller.promotion;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.promotion.topic.*;
import com.cskaoyan.service.promotion.TopicService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description:
 * @Author: WD created on 2021-09-17 16:32
 * @Version:
 */


@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam param, String title, String subtitle) {
        ShowTopicListVO list = topicService.getList(param, title, subtitle);
        return BaseRespVo.ok(list);
    }

    @RequiresPermissions(value ={"*","admin:topic:read"},logical = Logical.OR)
    @RequestMapping("read")
    public BaseRespVo getTopicInfo(Integer id) {
        GetTopicInfoVO topicInfo = topicService.getTopicInfo(id);
        return BaseRespVo.ok(topicInfo);
    }

    @RequiresPermissions(value ={"*","admin:topic:update"},logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo updateTopic(@RequestBody UpdateTopicBO topicBO) {
        UpdateTopicVO updateTopicVO = topicService.updateTopic(topicBO);

        return BaseRespVo.ok(updateTopicVO);

    }

    @RequiresPermissions(value ={"*","admin:topic:create"},logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo createTopic(@RequestBody CreateTopicBO topicBO) {
        CreateTopicVO topic = topicService.createTopic(topicBO);

        return BaseRespVo.ok(topic);
    }

    @RequiresPermissions(value ={"*"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo deleteTopic(@RequestBody DeleteTopicBO topicBO) {
        Integer id = topicBO.getId();
        Integer integer = topicService.deleteTopicNotReal(id);
        if (integer > 0) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("未知原因失败！");

    }

    @RequiresPermissions(value ={"*"},logical = Logical.OR)
    @RequestMapping("batch-delete")
    public BaseRespVo deleteTopics(@RequestBody Map ids) {

        List<Integer> idList = (List<Integer>) ids.get("ids");

        Integer integer = topicService.deleteTopicsByIdNotReal(idList);

        return BaseRespVo.ok();

    }
}
